<?php
class ControllerRestaurantRestaurantOption extends Controller {
	private $error = array();

	public function index() {
		$this->load->language('common/header');
		$this->load->language('restaurant/restaurant_option');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/option');

		$this->getList();
	}

	public function insert() {
		$this->load->language('common/header');
		$this->load->language('restaurant/restaurant_option');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/option');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_option->addOption($this->request->post);
				
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
				
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
				
			$this->redirect($this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('common/header');
		$this->load->language('restaurant/restaurant_option');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('menu/option');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_menu_option->editOption($this->request->get['option_id'], $this->request->post);
				
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
				
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
				
			$this->redirect($this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function delete() {
		$this->load->language('restaurant/restaurant_option');

		$this->document->setTitle($this->language->get('heading_title'));
			
		$this->load->model('menu/option');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $option_id) {
				$this->model_menu_option->deleteOption($option_id);
			}
				
			$this->session->data['success'] = $this->language->get('text_success');
				
			$url = '';
				
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
				
			$this->redirect($this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'od.name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
			
		$url = '';
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);
		 
		$this->data['breadcrumbs'][] = array(
   		'text'      => $this->language->get('text_restaurant'),
   					'href'      => '#',
   		      		'separator' => ' :: '
   		      		);
   		      		 
   		      		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => '#',
      		'separator' => ' :: '
      		);

      		$this->data['insert'] = $this->url->link('restaurant/restaurant_option/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
      		$this->data['delete'] = $this->url->link('restaurant/restaurant_option/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
      			
      		$this->data['options'] = array();

      		$data = array(
			'group'  => 'restaurant',
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
      		);

      		$this->load->model('setting/image');

      		$option_total = $this->model_menu_option->getTotalOptions('restaurant');

      		$results = $this->model_menu_option->getOptions($data);

      		foreach ($results as $result) {
      			$action = array();
      				
      			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('restaurant/restaurant_option/update', 'token=' . $this->session->data['token'] . '&option_id=' . $result['option_id'] . $url, 'SSL')
      			);

      			$this->data['options'][] = array(
				'option_id'  => $result['option_id'],
				'name'       => $result['name'],
				'image'      => $this->model_setting_image->resize($result['image'], 32, 32),
				'sort_order' => $result['sort_order'],
				'selected'   => isset($this->request->post['selected']) && in_array($result['option_id'], $this->request->post['selected']),
				'action'     => $action
      			);
      		}

      		$this->data['heading_title'] = $this->language->get('heading_title');

      		$this->data['text_no_results'] = $this->language->get('text_no_results');

      		$this->data['column_name'] = $this->language->get('column_name');
      		$this->data['column_image'] = $this->language->get('column_image');
      		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
      		$this->data['column_action'] = $this->language->get('column_action');

      		$this->data['button_insert'] = $this->language->get('button_insert');
      		$this->data['button_delete'] = $this->language->get('button_delete');

      		if (isset($this->error['warning'])) {
      			$this->data['error_warning'] = $this->error['warning'];
      		} else {
      			$this->data['error_warning'] = '';
      		}

      		if (isset($this->session->data['success'])) {
      			$this->data['success'] = $this->session->data['success'];

      			unset($this->session->data['success']);
      		} else {
      			$this->data['success'] = '';
      		}

      		$url = '';

      		if ($order == 'ASC') {
      			$url .= '&order=DESC';
      		} else {
      			$url .= '&order=ASC';
      		}

      		if (isset($this->request->get['page'])) {
      			$url .= '&page=' . $this->request->get['page'];
      		}

      		$this->data['sort_name'] = $this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . '&sort=od.name' . $url, 'SSL');
      		$this->data['sort_sort_order'] = $this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . '&sort=o.sort_order' . $url, 'SSL');

      		$url = '';

      		if (isset($this->request->get['sort'])) {
      			$url .= '&sort=' . $this->request->get['sort'];
      		}

      		if (isset($this->request->get['order'])) {
      			$url .= '&order=' . $this->request->get['order'];
      		}

      		$pagination = new Pagination();
      		$pagination->total = $option_total;
      		$pagination->page = $page;
      		$pagination->limit = $this->config->get('config_admin_limit');
      		$pagination->text = $this->language->get('text_pagination');
      		$pagination->url = $this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

      		$this->data['pagination'] = $pagination->render();

      		$this->data['sort'] = $sort;
      		$this->data['order'] = $order;

      		$this->template = 'restaurant/restaurant_option_list.tpl';
      		$this->children = array(
			'common/header',
			'common/footer'
			);

			$this->response->setOutput($this->render());
	}

	private function getForm() {
		// Mapping all language variables
		$this->data = array_merge($this->data, $this->language->getData());

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = array();
		}

		if (isset($this->error['option_value'])) {
			$this->data['error_option_value'] = $this->error['option_value'];
		} else {
			$this->data['error_option_value'] = array();
		}

		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
		);

		 
		$this->data['breadcrumbs'][] = array(
   			'text'      => $this->language->get('text_restaurant'),
   			'href'      => '#',
   		   	'separator' => ' :: '
   		   	);
   		   	 
   		   	$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
      		);
      		 
      		$this->data['breadcrumbs'][] = array(
   			'text'      => !isset($this->request->get['option_id']) ? $this->language->get('heading_title_insert') : $this->language->get('heading_title_update'),
   			'href'      => '',
       		'separator' => ' :: '
       		);

       		if (!isset($this->request->get['option_id'])) {
       			$this->data['action'] = $this->url->link('restaurant/restaurant_option/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
       		} else {
       			$this->data['action'] = $this->url->link('restaurant/restaurant_option/update', 'token=' . $this->session->data['token'] . '&option_id=' . $this->request->get['option_id'] . $url, 'SSL');
       		}

       		$this->data['cancel'] = $this->url->link('restaurant/restaurant_option', 'token=' . $this->session->data['token'] . $url, 'SSL');

       		$this->data['token'] = $this->session->data['token'];

       		if (isset($this->request->get['option_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
       			$option_info = $this->model_menu_option->getOption($this->request->get['option_id']);
       		}

       		$this->load->model('setting/language');

       		$this->data['languages'] = $this->model_setting_language->getLanguages(array('portal' => '1'));

       		if (isset($this->request->post['option_description'])) {
       			$this->data['option_description'] = $this->request->post['option_description'];
       		} elseif (isset($this->request->get['option_id'])) {
       			$this->data['option_description'] = $this->model_menu_option->getOptionDescriptions($this->request->get['option_id']);
       		} else {
       			$this->data['option_description'] = array();
       		}

       		if (isset($this->request->post['category'])) {
       			$this->data['category'] = $this->request->post['category'];
       		} elseif (!empty($option_info)) {
       			$this->data['category'] = $option_info['category'];
       		} else {
       			$this->data['category'] = '';
       		}

       		if (isset($this->request->post['type'])) {
       			$this->data['type'] = $this->request->post['type'];
       		} elseif (!empty($option_info)) {
       			$this->data['type'] = $option_info['type'];
       		} else {
       			$this->data['type'] = '';
       		}

       		if (isset($this->request->post['image'])) {
       			$this->data['image'] = $this->request->post['image'];
       		} elseif (!empty($option_info)) {
       			$this->data['image'] = $option_info['image'];
       		} else {
       			$this->data['image'] = '';
       		}

       		$this->load->model('setting/image');

       		if (!empty($this->data['image']) && file_exists(DIR_IMAGE . $this->data['image'])) {
       			$this->data['thumb'] = $this->model_setting_image->resize($this->data['image'], 100, 100);
       		} elseif (!empty($option_info) && !empty($option_info['image']) && file_exists(DIR_IMAGE . $option_info['image'])) {
       			$this->data['thumb'] = $this->model_setting_image->resize($option_info['image'], 100, 100);
       		} else {
       			$this->data['thumb'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);
       		}

       		if (isset($this->request->post['sort_order'])) {
       			$this->data['sort_order'] = $this->request->post['sort_order'];
       		} elseif (!empty($option_info)) {
       			$this->data['sort_order'] = $option_info['sort_order'];
       		} else {
       			$this->data['sort_order'] = '';
       		}

       		if (isset($this->request->post['option_value'])) {
       			$option_values = $this->request->post['option_value'];
       		} elseif (isset($this->request->get['option_id'])) {
       			$option_values = $this->model_menu_option->getOptionValueDescriptions($this->request->get['option_id']);
       		} else {
       			$option_values = array();
       		}

       		$this->load->model('setting/image');

       		$this->data['option_values'] = array();
       			
       		foreach ($option_values as $option_value) {
       			if ($option_value['image'] && file_exists(DIR_IMAGE . $option_value['image'])) {
       				$image = $option_value['image'];
       			} else {
       				$image = 'no_image.jpg';
       			}
       				
       			$this->data['option_values'][] = array(
				'option_value_id'          => $option_value['option_value_id'],
				'option_value_description' => $option_value['option_value_description'],
				'image'                    => $image,
				'thumb'                    => $this->model_setting_image->resize($image, 100, 100),
				'sort_order'               => $option_value['sort_order']
       			);
       		}

       		$this->data['no_image'] = $this->model_setting_image->resize('no_image.jpg', 100, 100);

       		$this->template = 'restaurant/restaurant_option_form.tpl';
       		$this->children = array(
			'common/header',
			'common/footer'
			);

			$this->response->setOutput($this->render());
	}

	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'restaurant/restaurant_option')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		foreach ($this->request->post['option_description'] as $language_id => $value) {
			if ((utf8_strlen($value['name']) < 1) || (utf8_strlen($value['name']) > 128)) {
				$this->error['name'][$language_id] = $this->language->get('error_name');
			}
		}

		if (($this->request->post['type'] == 'select' || $this->request->post['type'] == 'radio' || $this->request->post['type'] == 'checkbox') && !isset($this->request->post['option_value'])) {
			$this->error['warning'] = $this->language->get('error_type');
		}

		if (isset($this->request->post['option_value'])) {
			foreach ($this->request->post['option_value'] as $option_value_id => $option_value) {
				foreach ($option_value['option_value_description'] as $language_id => $option_value_description) {
					if ((utf8_strlen($option_value_description['name']) < 1) || (utf8_strlen($option_value_description['name']) > 128)) {
						$this->error['option_value'][$option_value_id][$language_id] = $this->language->get('error_option_value');
					}
				}
			}
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'restaurant/restaurant_option')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		$this->load->model('restaurant/restaurant');

		foreach ($this->request->post['selected'] as $option_id) {
			$restaurant_total = $this->model_restaurant_restaurant->getTotalRestaurantsByOptionId($option_id);

			if ($restaurant_total) {
				$this->error['warning'] = sprintf($this->language->get('error_restaurant'), $restaurant_total);
			}
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

}
?>